package com.module.app.widget.gallery

import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import com.module.app.base.BaseFragment
import com.module.app.base.BaseViewModel
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.bumptech.glide.request.transition.DrawableCrossFadeFactory
import com.module.app.R
import com.module.app.databinding.AppFragmentGalleryBinding
import com.module.frame.ext.postEvent
import com.module.frame.glide.GlideApp

class GalleryFragment : BaseFragment<BaseViewModel, AppFragmentGalleryBinding>() {


    companion object {
        private val DATA = GalleryFragment::class.java.simpleName + "_data"
        open fun getInstance(url: String): Fragment {
            val fragment = GalleryFragment()
            val bundle = Bundle()
            bundle.putString(DATA, url)
            fragment.arguments = bundle
            return fragment
        }
    }


    private val url: String by lazy {
        arguments?.getString(DATA) ?: ""
    }


    override fun bingViewModel() {

    }

    override fun onLazyLoad() {
    }

    override fun initListener() {
        super.initListener()
        mDatabind.ivImage.setOnClickListener {
            onClickImage()
        }
    }


    override fun initData() {
        super.initData()
        if (arguments != null) {
            com.module.app.utils.GlideUtils.loadNet(
                mDatabind.ivImage.getContext(),
                mDatabind.ivImage,
                url,
                R.color.transparent
            )
            GlideApp.with(mDatabind.ivImage.context)
                .load(url)
                .error(R.drawable.app_default_logo)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.color.transparent)
                .transition(
                    DrawableTransitionOptions.with(
                        DrawableCrossFadeFactory.Builder(150).setCrossFadeEnabled(true).build()
                    )
                )
                .listener(object : RequestListener<Drawable> {
                    override fun onLoadFailed(
                        e: GlideException?,
                        model: Any,
                        target: Target<Drawable>,
                        isFirstResource: Boolean
                    ): Boolean {
                        mDatabind.loading.visibility = View.GONE
                        return false
                    }

                    override fun onResourceReady(
                        resource: Drawable,
                        model: Any,
                        target: Target<Drawable>,
                        dataSource: DataSource,
                        isFirstResource: Boolean
                    ): Boolean {
                        mDatabind.loading.visibility = View.GONE
                        return false
                    }
                })
                .into(mDatabind.ivImage)
        }
    }

    private fun onClickImage() {
        postEvent(galleryClickFlow,true)
    }

}